home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / text / cmanual.lzh / ACM3.lzh / LowLevelGraphics / Example1.c < prev    next >
C/C++ Source or Header  |  1991-01-12  |  5KB  |  181 lines

  1. /* Example 1                                                           */
  2. /* This example shows how to create your own display, and fill it with */
  3. /* a lot of pixels in seven different colours.                         */
  4.  
  5.  
  6. #include <intuition/intuition.h>
  7. #include <graphics/gfxbase.h>
  8.  
  9.  
  10. #define WIDTH  640 /* 640 pixels wide (high resolution)                */
  11. #define HEIGHT 200 /* 200 lines high (non interlaced NTSC display)     */ 
  12. #define DEPTH    3 /* 3 BitPlanes should be used, gives eight colours. */
  13. #define COLOURS  8 /* 2^3 = 8                                          */
  14.  
  15.  
  16. struct IntuitionBase *IntuitionBase;
  17. struct GfxBase *GfxBase;
  18.  
  19.  
  20. struct View my_view;
  21. struct View *my_old_view;
  22. struct ViewPort my_view_port;
  23. struct RasInfo my_ras_info;
  24. struct BitMap my_bit_map;
  25. struct RastPort my_rast_port;
  26.  
  27.  
  28. UWORD my_color_table[] =
  29. {
  30.   0x000, /* Colour 0, Black       */
  31.   0x800, /* Colour 1, Red         */
  32.   0xF00, /* Colour 2, Light red   */
  33.   0x080, /* Colour 3, Green       */
  34.   0x0F0, /* Colour 4, Light green */
  35.   0x008, /* Colour 5, Blue        */
  36.   0x00F, /* Colour 6, Light Blue  */
  37.   0xFFF, /* Colour 7, White       */
  38. };
  39.  
  40.  
  41. void clean_up();
  42. void main();
  43.  
  44.  
  45. void main()
  46. {
  47.   UWORD *pointer;
  48.   int loop;
  49.   
  50.  
  51.   /* Open the Intuition library: */
  52.   IntuitionBase = (struct IntuitionBase *)
  53.     OpenLibrary( "intuition.library", 0 );
  54.   if( !IntuitionBase )
  55.     clean_up( "Could NOT open the Intuition library!" );
  56.  
  57.   /* Open the Graphics library: */
  58.   GfxBase = (struct GfxBase *)
  59.     OpenLibrary( "graphics.library", 0 );
  60.   if( !GfxBase )
  61.     clean_up( "Could NOT open the Graphics library!" );
  62.  
  63.  
  64.   /* Save the current View, so we can restore it later: */
  65.   my_old_view = GfxBase->ActiView;
  66.  
  67.  
  68.   /* 1. Prepare the View structure, and give it a pointer to */
  69.   /*    the first ViewPort:                                  */
  70.   InitView( &my_view );
  71.   my_view.ViewPort = &my_view_port;
  72.  
  73.  
  74.   /* 2. Prepare the ViewPort structure, and set some important values: */
  75.   InitVPort( &my_view_port );
  76.   my_view_port.DWidth = WIDTH;         /* Set the width.                */
  77.   my_view_port.DHeight = HEIGHT;       /* Set the height.               */
  78.   my_view_port.RasInfo = &my_ras_info; /* Give it a pointer to RasInfo. */
  79.   my_view_port.Modes = HIRES;          /* High resolution.              */
  80.  
  81.  
  82.   /* 3. Get a colour map, link it to the ViewPort, and prepare it: */
  83.   my_view_port.ColorMap = (struct ColorMap *) GetColorMap( COLOURS );
  84.   if( my_view_port.ColorMap == NULL )
  85.     clean_up( "Could NOT get a ColorMap!" );
  86.  
  87.   /* Get a pointer to the colour map: */
  88.   pointer = (UWORD *) my_view_port.ColorMap->ColorTable;
  89.  
  90.   /* Set the colours: */
  91.   for( loop = 0; loop < COLOURS; loop++ )
  92.     *pointer++ = my_color_table[ loop ];
  93.  
  94.  
  95.   /* 4. Prepare the BitMap: */
  96.   InitBitMap( &my_bit_map, DEPTH, WIDTH, HEIGHT );
  97.  
  98.   /* Allocate memory for the Raster: */ 
  99.   for( loop = 0; loop < DEPTH; loop++ )
  100.   {
  101.     my_bit_map.Planes[ loop ] = (PLANEPTR) AllocRaster( WIDTH, HEIGHT );
  102.     if( my_bit_map.Planes[ loop ] == NULL )
  103.       clean_up( "Could NOT allocate enough memory for the raster!" );
  104.  
  105.     /* Clear the display memory with help of the Blitter: */
  106.     BltClear( my_bit_map.Planes[ loop ], RASSIZE( WIDTH, HEIGHT ), 0 );
  107.   }
  108.  
  109.   
  110.   /* 5. Prepare the RasInfo structure: */
  111.   my_ras_info.BitMap = &my_bit_map; /* Pointer to the BitMap structure.  */
  112.   my_ras_info.RxOffset = 0;         /* The top left corner of the Raster */
  113.   my_ras_info.RyOffset = 0;         /* should be at the top left corner  */
  114.                                     /* of the display.                   */
  115.   my_ras_info.Next = NULL;          /* Single playfield - only one       */
  116.                                     /* RasInfo structure is necessary.   */
  117.  
  118.   /* 6. Create the display: */
  119.   MakeVPort( &my_view, &my_view_port );
  120.   MrgCop( &my_view );
  121.  
  122.  
  123.   /* 7. Prepare the RastPort, and give it a pointer to the BitMap. */
  124.   InitRastPort( &my_rast_port );
  125.   my_rast_port.BitMap = &my_bit_map;
  126.   
  127.  
  128.   /* 8. Show the new View: */
  129.   LoadView( &my_view );
  130.  
  131.  
  132.   /* Set the draw mode to JAM1. FgPen's colour will be used. */
  133.   SetDrMd( &my_rast_port, JAM1 );
  134.   /* Draw 10000 pixels in seven different colours, randomly. */ 
  135.   for( loop = 0; loop < 10000; loop++ )
  136.   {
  137.     /* Set FgPen's colour (1-7, 0 used for the the background). */
  138.     SetAPen( &my_rast_port, rand() % (COLOURS-1) + 1 );
  139.     /* Write a pixel somewere on the display: */
  140.     WritePixel( &my_rast_port, rand() % WIDTH, rand() % HEIGHT );
  141.   }
  142.  
  143.  
  144.   /* 9. Restore the old View: */
  145.   LoadView( my_old_view );
  146.  
  147.  
  148.   /* Free all allocated resources and leave. */
  149.   clean_up( "THE END" );
  150. }
  151.  
  152.  
  153. /* Returns all allocated resources: */
  154. void clean_up( message )
  155. STRPTR message;
  156. {
  157.   int loop;
  158.  
  159.   /* Free automatically allocated display structures: */
  160.   FreeVPortCopLists( &my_view_port );
  161.   FreeCprList( my_view.LOFCprList );
  162.   
  163.   /* Deallocate the display memory, BitPlane for BitPlane: */
  164.   for( loop = 0; loop < DEPTH; loop++ )
  165.     if( my_bit_map.Planes[ loop ] )
  166.       FreeRaster( my_bit_map.Planes[ loop ], WIDTH, HEIGHT );
  167.  
  168.   /* Deallocate the ColorMap: */
  169.   if( my_view_port.ColorMap ) FreeColorMap( my_view_port.ColorMap );
  170.  
  171.   /* Close the Graphics library: */
  172.   if( GfxBase ) CloseLibrary( GfxBase );
  173.  
  174.   /* Close the Intuition library: */
  175.   if( IntuitionBase ) CloseLibrary( IntuitionBase );
  176.  
  177.   /* Print the message and leave: */
  178.   printf( "%s\n", message ); 
  179.   exit();
  180. }
  181.